Skip to content

Fix double-free crash under Perl ithreads#198

Draft
toddr-bot wants to merge 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-ithread-double-free
Draft

Fix double-free crash under Perl ithreads#198
toddr-bot wants to merge 1 commit into
cpan-authors:mainfrom
toddr-bot:koan.toddr.bot/fix-ithread-double-free

Conversation

@toddr-bot
Copy link
Copy Markdown
Contributor

@toddr-bot toddr-bot commented May 26, 2026

What

Adds CLONE_SKIP to prevent Crypt::OpenSSL::RSA objects from causing
double-free crashes when Perl ithreads clone the interpreter.

Why

When threads->create() clones the interpreter, blessed IV references
are copied as raw integer values. Both parent and child threads end up
with the same rsaData* pointer. When each thread's DESTROY fires,
the same EVP_PKEY is freed twice — segfault or heap corruption.

This is the standard failure mode for XS modules holding C pointers
under ithreads. The static buffer fix in PR #136 addressed one aspect
of thread safety, but the object lifecycle issue remained.

How

CLONE_SKIP { 1 } tells Perl to undef objects of this class in child
threads rather than shallow-copying them. Child threads must create
their own key objects — a safer semantic than silent memory corruption.

Testing

  • New t/threads.t verifies CLONE_SKIP is defined and that child
    threads can independently generate keys without affecting the parent.
  • Test skips cleanly on non-ithread perls (CI matrix includes both).
  • Full test suite passes (668 tests).

🤖 Generated with Claude Code


Quality Report

Changes: 2 files changed, 52 insertions(+)

Code scan: clean

Tests: passed (OK)

Branch hygiene: clean

Generated by Kōan post-mission quality pipeline

When Perl ithreads clone an interpreter, blessed IV references (like
our rsaData* pointers) are copied as raw integers.  Both parent and
child threads then call DESTROY on the same EVP_PKEY, causing a
double-free.  CLONE_SKIP makes cloned objects undef in child threads
instead.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant